import React, { Component } from 'react';
import { Form, Select, Modal } from 'antd';
import Tips from '../../Tips/index';
import { connect } from 'react-redux';
const FormItem = Form.Item;
const Option = Select.Option;

class Addpeer extends Component {
  constructor() {
    super();
    this.state = {
      message: ''
    };
  }
  submit = (e) => {
    e.preventDefault();
    if (!this.props.changeNetwork) {
      this.setState({ message: '请先加入一个网络！' }, () => {
        setTimeout(() => {
          this.setState({ message: '' });
        }, 1000);
      });
    } else {
      this.props.addsubmit();
    }
  }

  render() {
    const { visible, onCancel, form, } = this.props;
    let changeNetwork = this.props.changeNetwork;
    let changeOrgid = Number(this.props.changeOrgid);
    let orglist = '';
    let networklist = '';
    if (this.props.organdnet && this.props.organdnet.org && this.props.organdnet.org.length != 0) {
      orglist = this.props.organdnet.org.map((e, i) => {
        return <Option key={i} value={e.id} >{e.orgName}</Option>;
      });
    } else {
      changeNetwork = '暂无组织';
      networklist = (<Option disabled value="暂无组织" >暂无组织</Option>);
    }
    if (this.props.organdnet && this.props.organdnet.net && this.props.organdnet.net.length !== 0) {
      networklist = this.props.organdnet.net.map((e, i) => {
        return <Option key={i} value={e.networkName} >{e.nickname}</Option>;
      });
    } else {
      changeNetwork = '暂无网络';
      networklist = (<Option disabled value="暂无网络" >暂无网络</Option>);
    }

    return <Modal visible={visible} title="新建Peer" okText="确认新建" cancelText="取消" closable={false} onCancel={onCancel} onOk={this.submit}>
      <Form>
        <FormItem label="配给组织" labelCol={{ span: 7 }} wrapperCol={{ span: 15 }}>
          <Select disabled value={changeOrgid} onChange={this.changeOrgid}>
            {orglist}
          </Select>
        </FormItem>
        <FormItem label="配给网络" labelCol={{ span: 7 }} wrapperCol={{ span: 15 }}>
          <Select disabled value={changeNetwork} onChange={this.changeNetwork}>
            {networklist}
          </Select>
        </FormItem>
      </Form>
      <Tips message={this.state.message} />
    </Modal>;
  }
}
const AddPeer = Form.create()(Addpeer);
export default connect((state) => {
  return { ...state };
})(AddPeer);
